package com.ruoyi.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.elasticsearch.domain.EsShopDoc;
import com.ruoyi.common.elasticsearch.service.EsShopDocService;
import com.ruoyi.common.oms.dao.YytOmsOrderItemMapper;
import com.ruoyi.common.oms.entity.YytOmsOrderItem;
import com.ruoyi.common.pms.entity.YytPmsSkuInfo;
import com.ruoyi.common.pms.entity.YytPmsSpuInfo;
import com.ruoyi.common.pms.service.YytPmsSkuInfoService;
import com.ruoyi.common.pms.service.YytPmsSpuInfoService;
import com.ruoyi.utils.PageResult;
import com.ruoyi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/home")
public class HomeSearchController {

    @Autowired
    private EsShopDocService esShopDocService;

    @Autowired
    private YytOmsOrderItemMapper yytOmsOrderItemMapper;

    @Autowired
    private YytPmsSpuInfoService yytPmsSpuInfoService;

    @Autowired
    private YytPmsSkuInfoService yytPmsSkuInfoService;


    @PostMapping("/searchHomeShop")
    public ResponseResult searchHomeShop(@RequestBody EsShopDoc esShopDoc){
        PageResult search = esShopDocService.search(esShopDoc);

        return ResponseResult.success(search);
    }

    /**
     * 获取热销商品
     * @return
     */
    @GetMapping("/getHotSale")
    public ResponseResult getHotSale(){
        return ResponseResult.success(yytOmsOrderItemMapper.getSkuHotSale());
    }


    /**
     * 获取秒杀商品
     * @return
     */
    @GetMapping("/getSeckillSku")
    public ResponseResult getSeckillSku(){
        QueryWrapper<YytPmsSpuInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("spu_add_type", 0).eq("publish_status", 1).eq("audit_status", "0").eq("is_activity_status", "1");
        List<YytPmsSpuInfo> list = yytPmsSpuInfoService.list(wrapper);
        List<YytPmsSkuInfo> resultList = new ArrayList<>();
        for (YytPmsSpuInfo spu: list){
            QueryWrapper<YytPmsSkuInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("spu_id", spu.getId());
            resultList.add(yytPmsSkuInfoService.getOne(queryWrapper, false));
        }

        return ResponseResult.success(resultList);
    }

}
